
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This file was initially generated by gen_to_start.go (add link), as a start
// of the Traffic Ops golang data model

package api

import (
	"encoding/json"
	_ "github.com/apache/incubator-trafficcontrol/traffic_ops/experimental/server/output_format" // needed for swagger
	"github.com/jmoiron/sqlx"
	"log"
	"time"
)

type ProfilesParameters struct {
	Profile     string                  `db:"profile" json:"profile"`
	ParameterId int64                   `db:"parameter_id" json:"parameterId"`
	CreatedAt   time.Time               `db:"created_at" json:"createdAt"`
	Links       ProfilesParametersLinks `json:"_links" db:-`
}

type ProfilesParametersLinks struct {
	Self string `db:"self" json:"_self"`
}

// @Title getProfilesParametersById
// @Description retrieves the profiles_parameters information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    ProfilesParameters
// @Resource /api/2.0
// @Router /api/2.0/profiles_parameters/{id} [get]
func getProfilesParameter(profile string, parameterId int64, db *sqlx.DB) (interface{}, error) {
	ret := []ProfilesParameters{}
	arg := ProfilesParameters{}
	arg.Profile = profile
	arg.ParameterId = parameterId
	queryStr := "select *, concat('" + API_PATH + "profiles_parameters', '/profile/', profile, '/parameter_id/', parameter_id) as self"
	queryStr += " from profiles_parameters WHERE profile=:profile AND parameter_id=:parameter_id"
	nstmt, err := db.PrepareNamed(queryStr)
	err = nstmt.Select(&ret, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	nstmt.Close()
	return ret, nil
}

// @Title getProfilesParameterss
// @Description retrieves the profiles_parameters
// @Accept  application/json
// @Success 200 {array}    ProfilesParameters
// @Resource /api/2.0
// @Router /api/2.0/profiles_parameters [get]
func getProfilesParameters(db *sqlx.DB) (interface{}, error) {
	ret := []ProfilesParameters{}
	queryStr := "select *, concat('" + API_PATH + "profiles_parameters', '/profile/', profile, '/parameter_id/', parameter_id) as self"
	queryStr += " from profiles_parameters"
	err := db.Select(&ret, queryStr)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return ret, nil
}

// @Title postProfilesParameters
// @Description enter a new profiles_parameters
// @Accept  application/json
// @Param                 Body body     ProfilesParameters   true "ProfilesParameters object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/profiles_parameters [post]
func postProfilesParameter(payload []byte, db *sqlx.DB) (interface{}, error) {
	var v ProfilesParameters
	err := json.Unmarshal(payload, &v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "INSERT INTO profiles_parameters("
	sqlString += "profile"
	sqlString += ",parameter_id"
	sqlString += ",created_at"
	sqlString += ") VALUES ("
	sqlString += ":profile"
	sqlString += ",:parameter_id"
	sqlString += ",:created_at"
	sqlString += ")"
	result, err := db.NamedExec(sqlString, v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title putProfilesParameters
// @Description modify an existing profiles_parametersentry
// @Accept  application/json
// @Param   id              path    int     true        "The row id"
// @Param                 Body body     ProfilesParameters   true "ProfilesParameters object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/profiles_parameters/{id}  [put]
func putProfilesParameter(profile string, parameterId int64, payload []byte, db *sqlx.DB) (interface{}, error) {
	var arg ProfilesParameters
	err := json.Unmarshal(payload, &arg)
	arg.Profile = profile
	arg.ParameterId = parameterId
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "UPDATE profiles_parameters SET "
	sqlString += "profile = :profile"
	sqlString += ",parameter_id = :parameter_id"
	sqlString += ",created_at = :created_at"
	sqlString += " WHERE profile=:profile AND parameter_id=:parameter_id"
	result, err := db.NamedExec(sqlString, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title delProfilesParametersById
// @Description deletes profiles_parameters information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    ProfilesParameters
// @Resource /api/2.0
// @Router /api/2.0/profiles_parameters/{id} [delete]
func delProfilesParameter(profile string, parameterId int64, db *sqlx.DB) (interface{}, error) {
	arg := ProfilesParameters{}
	arg.Profile = profile
	arg.ParameterId = parameterId
	result, err := db.NamedExec("DELETE FROM profiles_parameters WHERE profile=:profile AND parameter_id=:parameter_id", arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}
